package com.example.myapplicationndk.paging;

import android.os.Bundle;

import com.example.myapplicationndk.R;
import com.example.myapplicationndk.bean.Artical;

import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainPagingActivity extends AppCompatActivity {

    RecyclerView mRecycleview;
    private PagingAdapter mPagingAdapter;
    private LiveData<List<Artical>> list = null;
    private PagingViewModel.ArticleDataSource mDataSource;
    private String TAG = this.getClass().getName() + "111";
    private PagingViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_paging);

        mRecycleview = findViewById(R.id.mRecyclerView);
        mRecycleview.setLayoutManager(new LinearLayoutManager(this));

        mPagingAdapter = new PagingAdapter();
        mRecycleview.setAdapter(mPagingAdapter);


        viewModel = new ViewModelProvider(this).get(PagingViewModel.class);
        viewModel.getArticleLiveData().observe(this, new Observer<PagedList<ArticleResponse.DataBean.DatasBean>>() {
            @Override
            public void onChanged(PagedList<ArticleResponse.DataBean.DatasBean> datasBeans) {
                mPagingAdapter.submitList(datasBeans);
            }
        });

    }

}